package com.bitBusy.manageyourriches.database.queries;

import java.util.LinkedHashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.bitBusy.manageyourriches.database.tables.SubcategoryCategory;
import com.bitBusy.manageyourriches.utilityClasses.dialogboxes.DialogBoxPrompts;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayMessages;

/**
 * Class that holds all queries related to the subcategory-category table
 * @extends DatabaseQueries class
 * @implements the UpdateSubcategories class
 * @author SumaHarsha
 *
 */
public class QueriesSubcategoryCategoryTable extends DatabaseQueries 
			implements UpdateSubcategories{

	
	/** Name of table related to queries in this class*/
	private static final String tableName = SubcategoryCategory.class.getSimpleName();
	
	/** String for the category column name*/
	private static final String categoryCol = SubcategoryCategory.CATEGORYCOL.getName();
	
	/** String for the subcategory column name*/
	private static final String subCategoryCol = SubcategoryCategory.SUBCATEGORYCOL.getName();

	/** String for the pic id column name*/
	private static final String picIDCol = SubcategoryCategory.PICIDCOL.getName();

	/** String array of all column names, in this table*/
//	private static final String[] allColumns = new String[]{subCategoryCol, categoryCol};	
	
	/** Reference to the Cursor object used to store and process query results*/
	private Cursor cursor;

	/**
	 * Parameterized Constructor
	 * Calls the super constructor
	 * @param the_context the current View context
	 */
	public QueriesSubcategoryCategoryTable(Context the_context) {
		super(the_context);
	}

	/**
	 * Method to add more than one subcategory for a category
	 * @param the category name for which the subcategory names are to be added
	 */
	public void addSubcategories(String the_categoryName, Map<String, Integer> the_subcategories) {
	 
		if (the_categoryName != null &&  the_subcategories != null)
		{
			boolean iOpened = checkAndOpen();
			if (myDatabase != null)
			{
				try
				{
					ContentValues content = new ContentValues();
					for(String subcategoryName : the_subcategories.keySet())
					{
						content.put(subCategoryCol, subcategoryName);
						content.put(categoryCol, the_categoryName);
						content.put(picIDCol, the_subcategories.get(subcategoryName));
						myDatabase.insert(tableName, null, content);
					}
				}
				catch(SQLiteException exception)
				{
					DisplayMessages.displayUserError(myActivityContext, DialogBoxPrompts.DIALOGNONUNIQUECATNAME.getPrompt());
				}
				finally
				{
					checkAndClose(iOpened);
				}
			}
			else
			{
				if(myActivityContext != null)
				{
					DisplayMessages.displayInternalError(myActivityContext, DialogBoxPrompts.DIALOGNULLVALUERECEIVEDPROMPT.getPrompt());
				}
			}
		}
				
		else
		{
			if(myActivityContext != null)
			{
				DisplayMessages.displayInternalError(myActivityContext, DialogBoxPrompts.DIALOGNULLVALUERECEIVEDPROMPT.getPrompt());
			}
		}
		
	}
	

	/** Method to get all subcategory names for a given category
	 * @param the category name for which all subcategory names are to be returned
	 * @return the map of subcategory names and image ids for the given category name 
	 */
	public LinkedHashMap<String, Integer> getSubcategories(String the_categoryName)
	{
		LinkedHashMap<String, Integer> subcategories = null;
		boolean iOpened = checkAndOpen();
		cursor = myDatabase.query
		(tableName, new String[]{subCategoryCol, picIDCol},
				categoryCol+"=?",new String[]{the_categoryName}, null, null, null);
		if (cursor != null && cursor.moveToFirst())
		{
			subcategories = new LinkedHashMap<String, Integer>();
			while (!cursor.isAfterLast())
			{
				subcategories.put(cursor.getString(cursor.getColumnIndex(subCategoryCol)), cursor.getInt(cursor.getColumnIndex(picIDCol)));
				cursor.moveToNext();
			}
			cursor.close();
		}
		checkAndClose(iOpened);
		return subcategories;
	}

	/* (non-Javadoc)
	 * @see com.bitBusy.manageyourriches.database.queries.UpdateSubcategories#addSubcategories(java.lang.String, java.util.List, android.database.sqlite.SQLiteDatabase)
	 */
	@Override
	public void addSubcategories(String the_categoryName, Map<String, Integer> the_subcategories,
			SQLiteDatabase the_db) {
		if (the_categoryName != null &&  the_subcategories != null && the_db != null)
		{
			myDatabase = the_db;
			try
			{
				ContentValues content = new ContentValues();
				for(String subcategoryName : the_subcategories.keySet())
				{
					content.put(subCategoryCol, subcategoryName);
					content.put(categoryCol, the_categoryName);
					content.put(picIDCol, the_subcategories.get(subcategoryName));
					myDatabase.insert(tableName, null, content);
				}
			}
			
			catch(SQLiteException exception)
			{
				if(myActivityContext != null)
				{
					DisplayMessages.displayUserError(myActivityContext, DialogBoxPrompts.DIALOGNONUNIQUECATNAMESUBCATEGORY.getPrompt());
				}
			}
		}
		else
		{
			if (myActivityContext != null)
			{
				DisplayMessages.displayInternalError(myActivityContext, DialogBoxPrompts.DIALOGNULLVALUERECEIVEDPROMPT.getPrompt());
			}
		}
			
			
	}

	/**
	 * @param the_categoryChosen
	 */
	public boolean deleteCategory(String the_categoryChosen) {
		boolean success = false;
		if (the_categoryChosen != null)
		{
			boolean iOpened = checkAndOpen();
	    	if( myDatabase.delete(tableName, categoryCol + " = '" + the_categoryChosen +"'", null)  >= 1)
			{
				success = true;
			}
		checkAndClose(iOpened);
		}
		return success;
		
	}

	/**
	 * @param the_category
	 * @param the_subcategory
	 * @return
	 */
	public boolean deleteSubcategory(String the_category, String the_subcategory) {
		boolean success = false;
		if (the_subcategory != null)
		{
			boolean iOpened = checkAndOpen();
	    	if( myDatabase.delete(tableName, categoryCol + " = '" + the_category + "' AND " + 
	    						subCategoryCol + " = '" + the_subcategory +"'", null)  >= 0)
			{
				success = true;
			}
		checkAndClose(iOpened);
		}
		return success;
		}


	
}

